//
// Created by 郝羽 on 2018/2/7.
//

#ifndef RTC_XSWITCH2_COMPARE_HPP
#define RTC_XSWITCH2_COMPARE_HPP

#include <iomanip>
#include <iostream>

#include "dtw.hpp"
#include "input.hpp"

class Compare {
    int step_msec = 2000;
public:
    void Files(const std::string& f1_name, const std::string& f2_name) {
        int x = 0;
        Input i1;
        Input i2;
        i1.Open(f1_name, step_msec);
        i2.Open(f2_name, step_msec);

        DTW d(16000, step_msec, step_msec);
        double e = 0.0;
        int64_t msec = 0;
        int n = 0;
        while (true) {
            n++;
            auto f1 = i1.Read();
            auto f2 = i2.Read();
            if (!f1 || !f2) {
                break;
            }

            double di = 0.0;
            d.Distance(&di, f1, f2);
            e += di;

            double db = std::log10(std::sqrt(e/n)) * 20;
            std::clog << std::setw(8) << msec / 1000 << ": " << db << " dBov" << std::endl;
            msec += f1->nb_samples * 1000 / 16000;
        }
        double db = std::log10(std::sqrt(e/n)) * 20;
        std::clog << db << " dBov" << std::endl;
    }
};
#endif //RTC_XSWITCH2_COMPARE_HPP
